﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DAL
{
    public class Tbl_SanPham_DAL
    {
        public SqlConnection cn = null;
        public Tbl_SanPham_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public Tbl_SanPham_DAL(Tbl_SanPham_DAL sp)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
        public Tbl_SanPham_Collection Get(string where, int pageIndex, int pageSize,
           string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("GET_SANPHAM", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            Tbl_SanPham_Collection spCollection = new Tbl_SanPham_Collection();

            while (dr.Read())
            {
                Tbl_SanPham_Entities sp = new Tbl_SanPham_Entities();
                sp.STT = int.Parse(dr.GetValue(0).ToString());
                sp.MaSP = dr.GetValue(1).ToString();
                sp.TenSP = dr.GetValue(2).ToString();
                sp.GiaGoc = float.Parse(dr.GetValue(3).ToString());
                sp.GiaBan = float.Parse(dr.GetValue(4).ToString());
                sp.TyLeGiamGia = float.Parse(dr.GetValue(5).ToString());
                sp.NgayDang = dr.GetValue(6).ToString();
                sp.NgayBan = dr.GetValue(7).ToString();
                sp.NgayKetThucDuKien = dr.GetValue(8).ToString();
                sp.NgayKetThuc = dr.GetValue(9).ToString();
                sp.TinhTrang = dr.GetValue(10).ToString();
                sp.ThongTinCoBan = dr.GetValue(11).ToString();
                sp.ThongTinDayDu = dr.GetValue(12).ToString();
                sp.ThoiGianSuDung = dr.GetValue(13).ToString();
                sp.DKSD = dr.GetValue(14).ToString();
                sp.SLBan = int.Parse(dr.GetValue(15).ToString());
                sp.DoanhNghiep = int.Parse(dr.GetValue(16).ToString());
                sp.LoaiSP =  int.Parse(dr.GetValue(17).ToString());
                spCollection.Add(sp);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return spCollection;
        }

        public int insert(Tbl_SanPham_Entities sp)
        {
            SqlCommand cm = new SqlCommand("INSERT_SANPHAM", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MASP", sp.MaSP);
            cm.Parameters.AddWithValue("@TENSP", sp.TenSP);
            cm.Parameters.AddWithValue("@GIAGOC", sp.GiaGoc);
            cm.Parameters.AddWithValue("@GIABAN", sp.GiaBan);
            cm.Parameters.AddWithValue("@TYLEGIAMGIA", sp.TyLeGiamGia);
            cm.Parameters.AddWithValue("@NgayDang", sp.NgayDang);
            cm.Parameters.AddWithValue("@NgayBan", sp.NgayBan);
            cm.Parameters.AddWithValue("@NgayKetThucDuKien", sp.NgayKetThucDuKien);
            cm.Parameters.AddWithValue("@NgayKetThuc", sp.NgayKetThuc);
            cm.Parameters.AddWithValue("@TinhTrang", sp.TinhTrang);
            cm.Parameters.AddWithValue("@ThongTinCoBan", sp.ThongTinCoBan);
            cm.Parameters.AddWithValue("@ThongTinDayDu", sp.ThongTinDayDu);
            cm.Parameters.AddWithValue("@ThoiGianSuDung", sp.ThoiGianSuDung);
            cm.Parameters.AddWithValue("@DKSD", sp.DKSD);
            cm.Parameters.AddWithValue("@SLBan", sp.SLBan);
            cm.Parameters.AddWithValue("@DoanhNghiep", sp.DoanhNghiep);
            cm.Parameters.AddWithValue("@LoaiSP", sp.LoaiSP);
            cm.Parameters.Add(new SqlParameter("@STT", DbType.Int32)).Direction = ParameterDirection.Output;
            cm.ExecuteNonQuery();
            int stt = int.Parse(cm.Parameters["@STT"].Value.ToString());
            return stt;
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("DELETE_SANPHAM", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@STT", iD);
            cm.ExecuteNonQuery();
        }
        public void Update_hethan(Tbl_SanPham_Entities sp)
        {
            SqlCommand cm = new SqlCommand("UPDATE_SANPHAM_HETHAN", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@STT", sp.STT);
            cm.Parameters.AddWithValue("@TINHTRANG", sp.TinhTrang);
            cm.ExecuteNonQuery();
        }
        public void Update1(Tbl_SanPham_Entities sp)
        {
            SqlCommand cm = new SqlCommand("UPDATE_SANPHAM1", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@STT", sp.STT);
            cm.Parameters.AddWithValue("@TINHTRANG", sp.TinhTrang);
            cm.Parameters.AddWithValue("@NGAYKETTHUC", sp.NgayKetThuc);
            cm.ExecuteNonQuery();
        }
        public void Update2(Tbl_SanPham_Entities sp)
        {
            SqlCommand cm = new SqlCommand("UPDATE_SANPHAM2", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@STT", sp.STT);
            cm.Parameters.AddWithValue("@MASP", sp.MaSP);
            cm.Parameters.AddWithValue("@TENSP", sp.TenSP);
            cm.Parameters.AddWithValue("@GIAGOC", sp.GiaGoc);
            cm.Parameters.AddWithValue("@GIABAN", sp.GiaBan);
            cm.Parameters.AddWithValue("@TYLEGIAMGIA", sp.TyLeGiamGia);
            cm.Parameters.AddWithValue("@NgayDang", sp.NgayDang);
            cm.Parameters.AddWithValue("@NgayBan", sp.NgayBan);
            cm.Parameters.AddWithValue("@NgayKetThucDuKien", sp.NgayKetThucDuKien);
            cm.Parameters.AddWithValue("@NgayKetThuc", sp.NgayKetThuc);
            cm.Parameters.AddWithValue("@TinhTrang", sp.TinhTrang);
            cm.Parameters.AddWithValue("@ThongTinCoBan", sp.ThongTinCoBan);
            cm.Parameters.AddWithValue("@ThongTinDayDu", sp.ThongTinDayDu);
            cm.Parameters.AddWithValue("@ThoiGianSuDung", sp.ThoiGianSuDung);
            cm.Parameters.AddWithValue("@DKSD", sp.DKSD);
            cm.Parameters.AddWithValue("@SLBan", sp.SLBan);
            cm.Parameters.AddWithValue("@DoanhNghiep", sp.DoanhNghiep);
            cm.Parameters.AddWithValue("@LoaiSP", sp.LoaiSP);
            cm.ExecuteNonQuery();
        }
        public void Update_TinhTrang(Tbl_SanPham_Entities sp)
        {
            SqlCommand cm = new SqlCommand("UPDATE_SANPHAM1", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@STT", sp.STT);
            cm.Parameters.AddWithValue("@TINHTRANG", sp.TinhTrang);
            cm.ExecuteNonQuery();
        }
       
    }
}
